package com.example.demo.properties;

import org.springframework.boot.autoconfigure.condition.ConditionalOnClass;
import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean;
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
import org.springframework.boot.context.properties.EnableConfigurationProperties;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

@Configuration
@EnableConfigurationProperties(HelloProperties.class)
@ConditionalOnClass(HelloService.class)
public class HelloServiceAutoConfiguration {
    private final HelloProperties helloProperties;

    public HelloServiceAutoConfiguration(HelloProperties helloProperties) {
        this.helloProperties = helloProperties;
    }
    @Bean
    @ConditionalOnMissingBean
    @ConditionalOnProperty(prefix = "my.hello",value = "enabled",matchIfMissing = true)
    public HelloService helloService() {
        return new HelloService(this.helloProperties.getName(),
                                 this.helloProperties.getAge(),
                                    this.helloProperties.getHometown());
    }
}
